package com.cn.kabalifemerchants.ui.login;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.login.bean.AgreementBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.cn.kabalifemerchants.utils.WebViewHtmlUtils;
import com.cn.kabalifemerchants.utils.X5WebView;
import com.tencent.smtt.sdk.WebSettings;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/7/6.
 * function: 协议
 */
public class AgreementActivity extends IBaseActivity<AgreementView, AgreementPresenter> implements AgreementView {
    @BindView(R.id.webView)
    WebView webView;

    Bundle bundle;
    int sign;
    @BindView(R.id.ll_b)
    LinearLayout llB;
    @Override
    protected AgreementPresenter createPresenter() {
        return new AgreementPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_agreement;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        bundle = getIntent().getExtras();
        sign = bundle.getInt("sign");

        //5：商户隐私协议，6：代理隐私协议
        if (sign == 1)
            setTitleBar("注册协议");
        else if (sign == 2 || sign == 5 || sign == 6)
            setTitleBar("隐私协议");
        else if (sign == 3)
            setTitleBar("商户注册协议");
        mPresenter.onAgreementData(sign);
//        WebSettings webSettings = webView.getSettings();
//        webSettings.setJavaScriptEnabled(true);//允许使用js
//        //不支持屏幕缩放
//        webSettings.setSupportZoom(false);
//        webSettings.setBuiltInZoomControls(false);
//        //不显示webview缩放按钮
//        webSettings.setDisplayZoomControls(false);

        webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                float webViewContentHeight=webView.getContentHeight() * webView.getScale();
                float webViewCurrentHeight=(webView.getHeight() + webView.getScrollY());
                LoggerUtils.e("滑动监听：webView的高度："+webViewContentHeight+"现在的高度："+webViewCurrentHeight);
//                LoggerUtils.e("滑动监听：scrollX"+scrollX+"，scrollY："+scrollY+"，oldScrollX："+oldScrollX+"，oldScrollY："+oldScrollY);
                if (sign==1){
                    if (100>(webViewContentHeight-webViewCurrentHeight))
                    {
                        llB.setVisibility(View.VISIBLE);
                    }else {
                        llB.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

//    @SuppressLint("SetJavaScriptEnabled")
//    @Override
//    public void onResume() {
//        super.onResume();
//        webView.onResume();
//        webView.getSettings().setJavaScriptEnabled(true);
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        webView.onPause();
//        webView.getSettings().setLightTouchEnabled(false);
//    }
//
//    //销毁 放置内存泄漏
//    @Override
//    public void onDestroy() {
//        if (this.webView != null) {
//            webView.destroy();
//        }
//        super.onDestroy();
//    }
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (webView != null && webView.canGoBack()) {
//                webView.goBack();
//                return true;
//            } else
//                return super.onKeyDown(keyCode, event);
//        }
//
//
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void onReLoggedIn(String msg) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onAgreementSuccess(AgreementBean bean) {
        if (!TextUtils.isEmpty(bean.getResult())) {
            StringBuilder sb = new StringBuilder();
            sb.append(WebViewHtmlUtils.getHtmlData(bean.getResult()));
            webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);//加载html数据
        }
    }
    @OnClick({R.id.tv_btn_no, R.id.tv_btn_yes})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_no:
                RxBus.getDefault().post(new RxBusBean(1111, 1111));
                finish();
                break;
            case R.id.tv_btn_yes:
                RxBus.getDefault().post(new RxBusBean(1112, 1112));
                finish();
                break;
        }
    }
}
